package db;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import db.ConnectionClass;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Login() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		response.setContentType("text/html");
		System.out.println("Ollaan get-metodissa!");
		System.out.println("Username: "+username);
		System.out.println("Password: "+password);
		boolean connectionOK = false;
		boolean checkLogin = false;
		
		ConnectionClass connection = new ConnectionClass();
		connection.connect(username, password);
		
		if (connection.getConnection(username, password) != null) {
			connectionOK = true;
		}
		
		HttpSession session = request.getSession(false);
		
		if (session != null && session.getAttribute("sessiopaalla") != null) {
			checkLogin = true;
		} else if (connectionOK) {
			checkLogin = true;
			session = request.getSession(true);
			session.setAttribute("sessiopaalla", "on");
		} else {
			RequestDispatcher disp = request.getRequestDispatcher("loginfail.html");
			disp.forward(request, response);
		}
		if (checkLogin) {
			if (username.equals("vilila") && password.equals("salasana")) {
				RequestDispatcher disp = request.getRequestDispatcher("loginsuccess.html");
				disp.forward(request, response);
			}
			if (!username.equals("vilila") || !password.equals("salasana")) {
				RequestDispatcher disp = request.getRequestDispatcher("loginfail.html");
				disp.forward(request,response);
			}
		}
		
		PrintWriter out = response.getWriter();
		out.println("Morjens!");
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		response.setContentType("text/html");
		System.out.println("Ollaan get-metodissa!");
		System.out.println("Username: "+username);
		System.out.println("Password: "+password);
		boolean connectionOK = false;
		boolean checkLogin = false;
		
		ConnectionClass connection = new ConnectionClass();
		connection.connect(username, password);
		
		if (connection.getConnection(username, password) != null) {
			connectionOK = true;
		}
		
		HttpSession session = request.getSession(false);
		
		if (session != null && session.getAttribute("sessiopaalla") != null) {
			checkLogin = true;
		} else if (connectionOK) {
			checkLogin = true;
			session = request.getSession(true);
			session.setAttribute("sessiopaalla", "on");
		} else {
			RequestDispatcher disp = request.getRequestDispatcher("loginfail.html");
			disp.forward(request, response);
		}
		if (checkLogin) {
			if (username.equals("vilila") && password.equals("salasana")) {
				RequestDispatcher disp = request.getRequestDispatcher("loginsuccess.html");
				disp.forward(request, response);
			}
			if (!username.equals("vilila") || !password.equals("salasana")) {
				RequestDispatcher disp = request.getRequestDispatcher("loginfail.html");
				disp.forward(request,response);
			}
		}
		
		/*PrintWriter out = response.getWriter();
		out.println("Morjens!");
		out.close();*/
	}

}
